package com.lgy.controller;

import com.lgy.service.EsliveDeptPanelNewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dateCheck")
public class DateCheckController {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private EsliveDeptPanelNewService esliveDeptPanelNewService;
    /**
     * curl http://127.0.0.1:8081/es/dateCheck/checkDbRightData?day=2023-02-21
     * 判断数据库中的表数据是否在ES中都存在且是达标
     * @param day 日期
     * @return
     */
    @RequestMapping("/checkDbRightData")
    public String checkDbRightData(String day) {
        String sql = "SELECT DISTINCT watch_member_id FROM live_user_watch_report_log WHERE report_time = '" + day+ "'";
        List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);
        List<Long> totalRightUser = new ArrayList<>();
        for(Map<String, Object> map : data) {
            Long watchMemberId = (Long) map.get("watch_member_id");
            totalRightUser.add(watchMemberId);
        }
        List<Map<String, Object>> allEsData = esliveDeptPanelNewService.findAllEsData(day, 1);
        int i = 0;
        for(Long watchMemberId : totalRightUser) {
            Map<String, Object> map = allEsData.stream().filter(t -> Integer.valueOf((Integer) t.get("watchMemberId")).intValue() == watchMemberId.intValue()).findAny().orElse(null);
            if(null != map && !map.isEmpty()) {
                continue;
            } else {
                System.out.println("有问题的用户：" + watchMemberId);
                i++;
            }
        }
        return "执行结束，核对用户数：" + totalRightUser.size() + " 有问题用户个数: " + i;
    }

    /**
     * curl http://127.0.0.1:8081/es/dateCheck/checkDbEnterData?day=2023-02-21
     * 判断用户进入直播间数据是否在直播间存在
     * @param day
     * @return
     */
    @RequestMapping("/checkDbEnterData")
    public String checkDbEnterData(String day) {
        String sql = "SELECT * FROM live_user_watch_base WHERE date(create_time) = '" + day+ "'";
        List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);

        //查询所有ES中的数据
        List<Map<String, Object>> allEsData = esliveDeptPanelNewService.findAllEsData(day, null);
        int i = 0;
        for(Map<String, Object> d : data) {
            Long liveId = (Long) d.get("live_id");
            Integer num = (Integer) d.get("num");
            Long watchMemberId = (Long) d.get("watch_member_id");
            String imei = (String) d.get("imei");

            Map<String, Object> map = allEsData.stream().filter(t ->
                    Long.valueOf((Integer) t.get("watchMemberId")).longValue() == watchMemberId.longValue() &&
                            Integer.valueOf((Integer) t.get("num")).intValue() == num.intValue() &&
                            Long.valueOf((Integer) t.get("liveId")).longValue() == liveId &&
                            t.get("imei").toString().contains(imei)
                    ).findAny().orElse(null);

            if(null != map && !map.isEmpty()) {
                continue;
            } else {
                System.out.println("有问题的用户：" + watchMemberId);
                i++;
            }
        }

        return "执行结束，核对用户数：" + data.size() + " 有问题用户个数: " + i;
    }

    /**
     * curl http://127.0.0.1:8081/es/dateCheck/checkOaUserNameData
     * 判断OA账号是否有脏数据
     * @return
     */
    @RequestMapping("/checkOaUserNameData")
    public String checkOaUserNameData() {
        String sql = "SELECT * FROM xgj_user WHERE `status` = 0";
        List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);
        Map<String, List<Map<String, Object>>> collect = data.stream().collect(Collectors.groupingBy(t -> (String)t.get("login_name")));
        AtomicInteger i = new AtomicInteger();
        collect.forEach((key, list) -> {
            List<Map<String, Object>> temp = list.stream().filter(t -> (Integer) t.get("main_flag") == 1).collect(Collectors.toList());
            if(temp.isEmpty() || temp.size() > 1) {
                System.out.println("该登录账号有问题：" + key);
                i.incrementAndGet();
            }
        });
        return "有问题的账号个数: " + i;
    }
}
